﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Data;

namespace Agro.Library {
    #region JqGridResult
    public class JqGridResult : JqGridResult<Dictionary<string, object>> {
        #region 构造函数...
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示行数</param>
        /// <param name="recordCount">总记录数</param>
        /// <param name="dataTable"></param>
        public JqGridResult(int pageIndex, int pageSize, int recordCount, DataTable dataTable)
            : base(pageIndex, pageSize, recordCount, DataTableToDictionaryList(dataTable)) {

        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示行数</param>
        /// <param name="recordCount">总记录数</param>
        /// <param name="userData">自定义数据</param>
        /// <param name="dataTable"></param>
        public JqGridResult(int pageIndex, int pageSize, int recordCount, object userData, DataTable dataTable)
            : base(pageIndex, pageSize, recordCount, userData, DataTableToDictionaryList(dataTable)) {

        }
        #endregion

        #region DataTableToDictionaryList
        private static List<Dictionary<string, object>> DataTableToDictionaryList(DataTable dataTable) {
            if (dataTable == null)
                throw new ArgumentNullException("dataTable");
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row = null;
            foreach (DataRow dr in dataTable.Rows) {
                row = new Dictionary<string, object>();
                foreach (DataColumn dc in dataTable.Columns)
                    row.Add(dc.ColumnName, dr[dc.ColumnName]);
                rows.Add(row);
            }
            return rows;
        }
        #endregion
    }
    #endregion

    #region JqGridResult<T>
    public class JqGridResult<T> : ActionResult {
        private int _pageIndex = 1;
        private int _pageSize = 20;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示的行数</param>
        /// <param name="recordCount">总记录数</param>
        /// <param name="dataRows">数据</param>
        public JqGridResult(int pageIndex, int pageSize, int recordCount, IList<T> dataRows)
            : this(pageIndex, pageSize, recordCount, null, dataRows) { }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示的行数</param>
        /// <param name="recordCount">总记录数</param>
        /// <param name="userData">用户自定数据</param>
        /// <param name="dataRows">数据</param>
        public JqGridResult(int pageIndex, int pageSize, int recordCount, object userData, IList<T> dataRows) {
            _pageIndex = pageIndex;
            _pageSize = pageSize;
            RecordCount = recordCount;
            DataRows = dataRows;
            UserData = userData;
        }


        /// <summary>
        /// 每页显示的条数
        /// </summary>
        public int PageIndex {
            get { return _pageIndex; }
            set {
                if (value > 0) {
                    _pageIndex = value;
                }
            }
        }

        /// <summary>
        /// 每页显示的数量
        /// </summary>
        public int PageSize {
            get {
                return _pageSize;
            }
            set {
                if (value > 0) {
                    _pageSize = value;
                }
            }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount {
            get {
                if (RecordCount <= 0) return 1;
                return (RecordCount - 1) / PageSize + 1;
            }
        }
        /// <summary>
        /// 总计录数
        /// </summary>
        public int RecordCount { get; set; }
        /// <summary>
        /// 数据行
        /// </summary>
        public IList<T> DataRows { get; set; }
        /// <summary>
        /// 用户的自定义数据
        /// </summary>
        public object UserData { get; set; }

        public override void ExecuteResult(ControllerContext context) {
            JsonResult json = new JsonResult();
            json.Data = new { page = PageIndex, total = PageCount, records = RecordCount, rows = DataRows, userdata = UserData };
            json.ExecuteResult(context);
        }
    }
    #endregion
}
